
/**
 * @author Administrator
 *
 */
package com.bgybmi.controller.repair;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.bgybmi.common.Constants;
import com.bgybmi.config.ConfigEnum;
import com.bgybmi.controller.BaseController;
import com.bgybmi.controller.BxEnum;
import com.bgybmi.entity.BxCreate;
import com.bgybmi.entity.BxImgCreate;
import com.bgybmi.entity.JbPhone;
import com.bgybmi.sender.Sender;
import com.bgybmi.sender.WeChatFactory;
import com.bgybmi.service.BxService;
import com.bgybmi.thread.SendMsg;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.upload.UploadFile;

/**
 * @format index-->route view-->action-->extra
 * @author hecore 
 * @version Improve:give up nav. invalid code for nav
 */
public class BxController extends BaseController{
	//消息记录的传输,消息记录的问题,模式1再次请求,模式二一次传输。
	//index 
	public final static String baseurl="/getOathUrl?subUrl="+BxEnum.bxinfo;
	//config配置
	public static final String agentId=PropKit.get(ConfigEnum.BX.getAgentId());
	//1.Elements 1 workflow img&text
	//2.msg Record-->List
	public void index(){
		JsVerify(agentId);
		jspAttr();
				//Constants.loginuser;//这个好像拿到的不对劲
		renderJsp("bxsq.jsp");			
	}
	//route view
	
	public void bxsq(){
		JsVerify(agentId);
		jspAttr();
		renderJsp("bxsq.jsp");
	}
	
	public void bxxq(){
		String id=getPara("id");
		JsVerify(agentId);
		jspAttr();
		renderJsp("bxxq.jsp");
	}
	
	public void mybx(){
		String status=getPara("status");//A--E
		JsVerify(agentId);
		jspAttr();
		String loginuser=getSessionAttr("loginuser");
		setAttr("loginuser",loginuser);
		renderJsp("mybx.jsp");
	}
	
	public void bxSql(){
		JsVerify(agentId);
		jspAttr();
				//Constants.loginuser;//这个好像拿到的不对劲
		renderJsp("bxsq.jsp");			
	}
	
	public void bxCreate(){
		JsVerify(agentId);
		jspAttr();		
		renderJsp("bxcreate.jsp");
	}
	
	//route nav  
	public void btn1_nav(){
		JsVerify(agentId);
		jspAttr();
		renderJsp("btn1_.jsp");		
	}
	
	//action->render
	public void bxSqNav(){
		JsVerify(agentId);
		jspAttr();
		renderJsp("bxsq.jsp");	
	}
	
	public void myBxNav(){
		JsVerify(agentId);
		jspAttr();
		renderJsp("mybx.jsp");
	}
	//action->curd
	
	public void bxCreateSubmit(){
		String loginuser=getSessionAttr("loginuser");
		Boolean saveflag=false;
		String bxr=getPara("bxr");
		String phone=getPara("phone");
		String bxposition=getPara("bxposition");	
		String bxdetail=getPara("bxdetail");	
		Boolean urgent=getParaToBoolean("urgent");//boolean
		String serverIds=getPara("serverid");
		String url="http://" + getRequest().getServerName()+baseurl;
				//"/oathSub?"+"repair/mybx";
		saveflag=BxService.me.bxCreateSubmit(loginuser,bxr,phone,bxposition,bxdetail,urgent,serverIds,url);				
		//图片信息的存入-->处理图像接口.
		setAttr("base","save");
		setAttr("res",saveflag);
		renderJson(); 			
	}
	
	//serverIds[][]={ZZzCnP-UAId-LOnDTgo0D9rQkoFYdAOtWpA9bM5OpjxIzlUsL1sZkkfxffC1pACF,mGY2c-y9XVSE33vBlwpI2DQhmyAUAALFts2De4HFTeOYpFuyr7wzobvBDq6zYkI3,F5evgB_meCYjDYe73xJYVhtrenuIS9WSeSvqHrMIGXgwiRwCIV2rSTDFxzdlx44C,kGURevC3yS378H7tDYpXwDQtyOGkeHFtusEusa94Olgg0xLwLVhIvBnQwUuHRc_U,RskNw1LqG6_VVB0nZAN2h2dG_-DXRreLMiCecorUT2ZtPwxrCBKA6FkQnf9D9H6o}  
	public void uploadData(){
		String serverIds=getPara("server");
		BxThread bx=new BxThread(serverIds,1);
		bx.start();
	}
	
	
	
	public void BxXqConfirm(){
		String loginuser=getSessionAttr("loginuser");
		String url="http://" + getRequest().getServerName()+baseurl;
		Boolean saveflag=false;		
		int id=getParaToInt("id");
		saveflag=BxService.me.BxXqConfirm(id,loginuser,url);
		//图片信息的存入-->处理图像接口.
		setAttr("base","update");
		setAttr("res",saveflag);
		renderJson(); 
	}
	
	public void BxXqCancel(){
		String loginuser=getSessionAttr("loginuser");
		String url="http://" + getRequest().getServerName()+baseurl;
		Boolean saveflag=false;
		int id=getParaToInt("id");
		saveflag=BxService.me.BxXqCancel(id,loginuser,url);
		//图片信息的存入-->处理图像接口.
		setAttr("base","update");
		setAttr("res",saveflag);
		renderJson(); 
	}
	
	public void wxUpload(){
		String serverId=getPara("serverId");
		String id=getPara("id");		
	}
	
	//VerSion 1 base upload by upload
	//OMKA3C_g-wqBo3FygsmoOaDpZ35AVzhof3xnfa77-mrPsGIH18zoBJxZUATZ4LH
	public void ImgUpload(){
		String serverId=getPara("serverId");
		String id=getPara("id");
		//通过ServerId去查找
		//JfInal温江上传代码
		UploadFile file=getFile("upload");
		   try
	        {
	            if (!empty(file))
	            {
	                String uploadContentType = file.getContentType();
	                if (!uploadContentType.equals("image/png")
	                        && !uploadContentType.equals("image/jpeg")
	                        && !uploadContentType.equals("image/pjpeg")
	                        && !uploadContentType.equals("image/x-png"))
	                {
	                    message = "上传文件类型不符合要求，暂时只支持png和jpg的图片。";
	                    //setAttr("message", message);
	                    renderJson(message);
	                    //renderJsp("/WEB-INF/content/common/result.jsp");
	                    return;
	                }
	                else
	                {
	                    String[] suffixarr = file.getFileName().split("\\.");
	                    String suffix = suffixarr[suffixarr.length - 1];
	                    String filename = UUID() + "." + suffix;
	                    file.getFile().renameTo(new File(PathKit.getWebRootPath()
	                            + "/attached/repair/" + filename));
	                    //信息存入数据库
	                    BxImgCreate bic=new BxImgCreate();
	                    bic.set("bx_id", id);
	                    bic.set("imgpath",PathKit.getWebRootPath() + "/attached/repair/" + filename);
	                    bic.set("imgname",filename);
	                    bic.set("imgformat",uploadContentType);
	                    bic.save();
	                    message = "上传成功";
	                    renderJson(message);
	                   // b.set("image", filename);
	                }
	            }

	        }
	        catch (Exception e)
	        {
	            message = "文件上传发生异常，请排除原因后再试";
	            setAttr("message", message);
	            renderJson(message);
	            return;
	        }
	        finally
	        {
	            if (!empty(file))
	            {
	                File oldfile = file.getFile();
	                if (oldfile.exists())
	                {
	                    oldfile.delete();
	                }
	            }
	        }
	}
	//extra
	//1.view_one
	public void listPhone(){
		List<JbPhone> li=BxService.me.listAllPhone();
		renderJson(li);
	}
	//2.view_two
	
	//no controller Method.
	/*
	 * 报销记录
	 * */
	public void listAllByUser(){
		String user=getSessionAttr("loginuser");
		List<BxCreate> li=BxService.me.listAllByUser(user);
		renderJson(li);
	}
	
	public void listAllByParamAndUser(){
		String status=getPara("status");
		String user=getSessionAttr("loginuser");
		List<BxCreate> li = BxService.me.listAllByParamAndUser(status,user);
		renderJson(li);
	}
	
	public void inBxRecord(){
		String user=getSessionAttr("loginuser");
		List<Map> result = BxService.me.bxmsg(user);
		renderJson(result);
	}
	
	public void bxXqRecord(){
		int id=getParaToInt("id");
		Map results=BxService.me.bxXqRecord(id);
		renderJson(results);
	}
	

}